<template>
  <!--  公话- 统计报表-  班级充值统计 -->
  <div class="class_recharge_statistics">
    <el-form :inline="true" :model="elform" class="demo-form-inline" size="mini">
      <el-form-item label="班级名称:">
        <el-select v-model="departmentGuid" placeholder="请选择" size="mini" clearable style="width:140px">
          <el-option
            v-for="item in departmentoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期:">
        <el-date-picker
          v-model="datetime[0]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[0]"
          value-format="HH:mm:ss"
          placeholder="开始时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item label="结束日期:">
        <el-date-picker
          v-model="datetime[1]"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
          style="width: 130px"
          size="mini"
          :clearable="false"
        ></el-date-picker>
        <el-time-picker
          style="width:110px"
          v-model="time[1]"
          value-format="HH:mm:ss"
          placeholder="结束时间"
          :clearable="false"
          size="mini"
        ></el-time-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="gosearch" size="mini" icon="el-icon-search">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportexeclto" size="mini">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="tableinformation"
      ref="table"
      style="width: 100%"
      border="border"
      :header-cell-style="headFirst"
      highlight-current-row
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column prop="departmentName" label="班级名称"></el-table-column>
      <el-table-column prop="adjustAmount" label="充值总金额(元)">
        <template slot-scope="scope">
          <div>{{ scope.row.adjustAmount | amountjiao }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="moneyCount" label="充值次数"></el-table-column>
      <el-table-column prop="moneyAvg" label="平均充值金额(元)">
        <template slot-scope="scope">
          <div>{{ scope.row.moneyAvg | amountjiao }}</div>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChangepage"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      elform: {},
      departmentoptions: [],
      departmentGuid: '',
      dataListLoading: false,
      currentPage: 1,
      recordCount: 0,
      pageSize: 20,
      pageNum: 1,
      // 时间
      datetime: [],
      time: [],
      tableinformation: [],
      userGuid: '',
      searchInfo: {},
      agentlistout: [],
      totalto: false,
      alladjustAmount: '',
      allmoneyCount: '',
      allmoneyAvg: '',
      hejilist: {},
      filename: '班级充值统计'
    }
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid
      }
    }
  },
  created() {
    this.datetime = [this.getNowTime('yy-mm-dd'), this.getNowTime('yy-mm-dd')]
    this.time = ['00:00:00', '23:59:59']
  },
  mounted() {
    this.departmentselectlist()
    this.classcspadjustWidth()
    window.addEventListener('resize', this.classcspadjustWidth.bind(this))
  },
  methods: {
    //合计
    classcspadjustWidth() {},
    gosearch() {
      this.pageNum = 1
      this.searchInfo = {}
      if (this.departmentGuid !== '' && this.departmentGuid !== null) {
        this.searchInfo.departmentGuid = this.departmentGuid
      }
      this.searchInfo.startDate = this.datetime[0] + ' ' + this.time[0]
      this.searchInfo.endDate = this.datetime[1] + ' ' + this.time[1]
      this.searchInfo.unitGuid = this.changeschoolid
      this.topupclassinit()
    },
    exportexeclto() {
      if (this.tableinformation.length == 0) {
        this.$message({
          message: '查询数据后导出',
          type: 'warning'
        })
        return false
      }
      this.outexecel()
    },
    outexecel() {
      if (this.tableinformation.length == 0) {
        this.$message({
          message: '查询数据后导出',
          type: 'warning'
        })
        return false
      }
      this.$http({
        url: this.$http.adornUrl('/user/recharge/detail/info-detail-dap'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageSize: this.recordCount,
            searchInfo: this.exportpam
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          let jsonArr = data.messageContent.data
          for (var i = 0; i < jsonArr.length; i++) {
            if (jsonArr[i].adjustAmount != null) {
              jsonArr[i].adjustAmount = this.amountjia(jsonArr[i].adjustAmount)
            }
            if (jsonArr[i].moneyAvg != null) {
              jsonArr[i].moneyAvg = this.amountjia(jsonArr[i].moneyAvg)
            }
          }
          jsonArr.push(this.hejilist)
          this.agentlistout = jsonArr
          setTimeout(() => {
            this.export2Excel()
          }, 100)
        } else {
          this.$message.error('导出遇未知错误')
        }
      })
    },
    export2Excel() {
      const nowtime = this.getimeto()
      require.ensure([], () => {
        const multiHeader = []
        const multiHeader2 = []
        const merges = []
        const { export_json_to_excel } = require('../../../excel/Export2Excel')
        const tHeader = [
          '所属班级',
          '充值总金额(元)',
          '充值次数',
          '平均充值金额(元)'
        ]
        const filterVal = [
          'departmentName',
          'adjustAmount',
          'moneyCount',
          'moneyAvg'
        ]
        const list = this.agentlistout
        const data = this.formatJson(filterVal, list)
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: this.filename + nowtime
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleSizeChangepage(val) {
      this.pageSize = val
      this.topupclassinit()
    },
    handleCurrentChangepage(val) {
      this.pageNum = val
      this.topupclassinit()
    },
    //部门选择
    departmentselectlist() {
      this.departmentoptions = []
      this.$http({
        url: this.$http.adornUrl('/department/query-department-info'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            unitGuid: this.changeschoolid
          },
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          console.log(data)
          let Arr = data.messageContent
          for (var i = 0; i < Arr.length; i++) {
            let itemobj = {
              value: Arr[i].departmentGuid,
              label: Arr[i].departmentName
            }
            this.departmentoptions.push(itemobj)
          }
        } else {
          this.$message.error(data.errorMessage)
        }
      })
    },
    //班级充值统计
    topupclassinit() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/user/recharge/detail/info-detail-dap'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo
          },
          messageType: this.messageType
        })
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          this.tableinformation = data.messageContent.data
          this.recordCount = data.messageContent.recordCount
          this.currentPage = data.messageContent.pageNum
          this.dataListLoading = false
          this.searchtotal()
        }
        if (data.messageCode === 500) {
          this.$message.error('查询遇未知错误')
        }
      })
    },
    searchtotal() {
      this.alladjustAmount = ''
      this.allmoneyCount = ''
      this.allmoneyAvg = ''
      this.$http({
        url: this.$http.adornUrl('/user/recharge/detail/info-detail-dap-total'),
        method: 'post',
        data: this.$http.adornData({
          messageContent: {
            searchInfo: this.searchInfo
          },
          messageType: 0
        })
      }).then(({ data }) => {
        console.log(data)
        if (data.messageCode === 200) {
          if (data.messageCode === 200 && data.messageContent.length != 0) {
            this.totalto = true
            this.alladjustAmount = this.amountjia(
              data.messageContent.adjustAmount
            )
            this.allmoneyCount = data.messageContent.moneyCount
            this.allmoneyAvg = this.amountjia(data.messageContent.moneyAvg)
            //
            // this.hejilist.unitName = "合计";
            this.hejilist.departmentName = '合计'
            this.hejilist.adjustAmount = this.alladjustAmount
            this.hejilist.moneyCount = this.allmoneyCount
            this.hejilist.moneyAvg = this.allmoneyAvg

            this.tableinformation.push({
              departmentName: '合计',
              adjustAmount: data.messageContent.adjustAmount,
              moneyCount: data.messageContent.moneyCount,
              moneyAvg: data.messageContent.moneyAvg
            })
          }
        }
        if (data.messageCode === 500) {
          this.totalto = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.class_recharge_statistics {
  padding: 10px;
  /deep/.el-table__empty-block {
    display: none;
  }
  .demo-form-inline,
  .demo-form-inline1 {
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 180px;
    }
  }
  .sum_footer {
    display: flex;
    display: -webkit-flex;
    line-height: 50px;
    color: #606266;
  }
  .sum_footer_unit {
    // border-top: 1px solid #ebeef5;
    // border-right: 1px solid #ebeef5;
    flex-grow: 1;
    -webkit-flex-grow: 1;
    text-indent: 10px;
    font-size: 14px;
  }
  .sum_footer_unit.center {
    text-align: center;
    padding-left: 1px;
  }
  .sum_footer.xiaoji {
    border-bottom: 1px solid #ebeef5;
  }
  .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>